package com.jaks.xpense;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.jaks.xpense.data.Expenses;
import com.jaks.xpense.data.Incomes;
import com.jaks.xpense.data.PaymentTypes;
import com.jaks.xpense.data.entities.Income;
import com.jaks.xpense.data.entities.PaymentType;
import com.jaks.xpense.data.entities.TableEntity;
import com.jaks.xpense.utils.Globals;
import com.jaks.xpense.utils.WidgetUtils;

public class EditIncomeActivity extends Activity implements OnClickListener {

	private PaymentTypes paymentTypes;
	private Incomes incomes;

	private ArrayList<PaymentType> paymentTypesList;
	private long id;
	private long paymentTypeId;

	private TextView incomeDate;
	private EditText amount;
	private TextView paidThru;
	private EditText comments;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		id = 0;
		paymentTypeId = 0;
		paymentTypes = new PaymentTypes(this);
		incomes = new Incomes(this);

		// UI Part
		setContentView(R.layout.manageincome);

		incomeDate = (TextView) findViewById(R.id.incomeDate);
		amount = (EditText) findViewById(R.id.incomeAmount);
		paidThru = (TextView) findViewById(R.id.incomeThru);
		comments = (EditText) findViewById(R.id.incomeComments);
		TextView saveIncome = (TextView) findViewById(R.id.saveIncome);

		incomeDate.setOnClickListener(this);
		paidThru.setOnClickListener(this);
		saveIncome.setOnClickListener(this);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			id = extras.getLong(TableEntity.Id);
			setTitle(R.string.editIncome);
		} else {
			setTitle(R.string.newIncome);
		}
		registerForContextMenu(paidThru);
		refreshPaymentTypes();
		populate();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	private void populate() {
		if (id == 0) {
			incomeDate.setText(Globals.toDateText(Calendar.getInstance()));

			if (paymentTypesList.size() > 0) {
				setPaymentType(paymentTypesList.get(0));
			}

			return;
		}

		Income income = (Income) incomes.read(id);
		incomeDate.setText(income.getIncomeDate());
		amount.setText(String.valueOf(income.getAmount()));
		comments.setText(income.getComments());
		setPaymentType(income.getPaymentType());
	}

	private void refreshPaymentTypes() {
		paymentTypesList = paymentTypes.readAllToList();
	}

	private void setPaymentType(long id) {

		for (PaymentType type : paymentTypesList) {
			if (type.getId() == id) {
				setPaymentType(type);
				break;
			}
		}
	}

	private void setPaymentType(PaymentType type) {
		paymentTypeId = type.getId();
		paidThru.setText(getString(R.string.thru) + " " + type.getName());
	}

	private boolean saveIncome() {

		String date = incomeDate.getText().toString();
		String comment = comments.getText().toString();
		if (amount.getText().toString().trim().length() == 0)
			return false;
		double incomeAmount = Double.parseDouble(amount.getText().toString());
		if (incomeAmount == 0.0)
			return false;

		Income income = new Income(id, date, paymentTypeId, incomeAmount, comment);
		if (id == 0) {
			id = incomes.create(income);
		} else {
			incomes.update(income);
		}

		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		// menu.clear();
		menu.setHeaderTitle(R.string.paymentTypes);
		menu.setHeaderIcon(null);
		for (PaymentType paymentType : paymentTypesList) {
			menu.add(0, (int) paymentType.getId(), 0, paymentType.getName());
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		setPaymentType(item.getItemId());
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		paymentTypes.close();
		incomes.close();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
			case R.id.incomeDate:
				Calendar calendar = Globals.toCalendar(incomeDate.getText().toString());
				int year = calendar.get(Calendar.YEAR);
				int month = calendar.get(Calendar.MONTH);
				int day = calendar.get(Calendar.DAY_OF_MONTH);

				DatePickerDialog datePicker = new DatePickerDialog(this, dateSetListener, year, month, day);
				datePicker.show();
				break;
			case R.id.incomeThru:
				openContextMenu(paidThru);
				break;
			case R.id.saveIncome:
				if (saveIncome()) {

					Expenses expenses = new Expenses(this);

					double todayTotal = expenses.getTodaysTotal();
					double balance = expenses.getBalance();

					expenses.close();

					WidgetUtils.sendUpdateToWidget(this, todayTotal, balance);

					setResult(RESULT_OK);
					finish();
				}
				break;
		}
	}

	private final DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int month, int day) {
			incomeDate.setText(Globals.toDateText(year, month, day));
		}
	};
}
